package oocis.information;

import java.io.*;

public class Storage {

	public Storage() {
		infoDir.mkdir();
	}

	static public void writeFile(String fname, String buf) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(fname));
			out.write(buf);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static public String readFile(String fname) {
		String rt = "";
		String line;
		try {
			BufferedReader in = new BufferedReader(new FileReader(fname));
			while ((line = in.readLine()) != null)
				rt += line;
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rt;
	}

	static public void infoStore(Information i) {
		File dir = new File(dirName + "/" + i.infoId);
		dir.mkdir();

		writeFile(dirName + "/" + i.infoId + "/" + head, i.info.infoHead);
		writeFile(dirName + "/" + i.infoId + "/" + abs, i.info.infoAbstract);
		writeFile(dirName + "/" + i.infoId + "/" + body, i.info.infoBody);
	}

	static public void infoLoad(Information i) {
		i.info.infoHead = readFile(dirName + "/" + i.infoId + "/" + head);
		i.info.infoAbstract = readFile(dirName + "/" + i.infoId + "/" + abs);
		i.info.infoBody = readFile(dirName + "/" + i.infoId + "/" + body);
	}

	static final String dirName = "InforStorage";

	static final String head = "head", abs = "abs", body = "body";

	static final File infoDir = new File(dirName);

}
